<script lang="jsx" setup>
import { ref } from 'vue';
import Form from '@/components/common/form.vue';
import { getModelList } from '@/service/api/modules/workwear/mold-maintenance';

const props = defineProps({
  rowData: {
    type: Object,
    default: () => null
  },
  operateType: {
    type: String,
    default: () => 'add'
  }
});

const moldNoList = ref([]);

function getMoldNoList() {
  getModelList({
    pageNum: 1,
    pageSize: 9999
  }).then(({ data, error }) => {
    if (!error) {
      moldNoList.value = data.list.map(item => ({
        value: item.moldNo,
        label: item.moldNo
      }));
    }
  });
}

getMoldNoList();

const formList = [
  {
    type: 'tabs',
    name: '基本项目',
    span: 24
  },
  {
    type: 'input',
    key: 'model',
    label: '型号',
    placeholder: '请输入型号',
    span: 24,
    rule: {
      required: true,
      message: '请输入型号',
      trigger: 'blur'
    }
  },

  {
    type: 'input',
    key: 'position',
    label: '存放位置',
    placeholder: '请输入存放位置',
    span: 24,
    rule: {
      required: true,
      message: '请输入存放位置',
      trigger: 'blur'
    }
  },

  {
    type: 'input',
    key: 'applicant',
    label: '申请人',
    placeholder: '请输入申请人',
    span: 24,
    rule: {
      required: true,
      message: '请输入申请人',
      trigger: 'blur'
    }
  },
  {
    type: 'select',
    key: 'states',
    label: '状态',
    placeholder: '请选择状态',
    span: 24,
    defaultValue: '1',
    selectOptions: [
      {
        value: '1',
        label: '生效'
      },
      {
        value: '2',
        label: '报废'
      }
    ]
  },

  {
    type: 'textarea',
    key: 'notes',
    label: '备注',
    placeholder: '请输入备注',
    span: 24,
    textareaRows: 1
  }
];

function getModel() {
  const obj = {};
  formList.forEach(item => {
    if (item.key && item.span) {
      if (props.operateType === 'add') {
        obj[item.key] = item.defaultValue || null;
      } else {
        obj[item.key] = props.rowData[item.key];
      }
    }
  });
  return obj;
}
const formRef = ref(null);

const model = ref(getModel());

function validateForm() {
  return formRef.value.validateForm();
}

defineExpose({
  model: model.value,
  validateForm
});
</script>

<template>
  <div class="event-form">
    <Form ref="formRef" v-model:model="model" :form-list="formList"></Form>
  </div>
</template>

<style lang="scss" scoped></style>
